/*
 * OptionPaneDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "OptionPaneDelegate.h"

#include "com_cute4j_swing_OptionPane.h"

#include "ComponentDelegate.h"

// Macro Helper. ---------------------------------------------------------------

#define MESSAGE_TYPE(type) com_cute4j_swing_OptionPane_##type

// OptionPaneDelegate :: Static Native Calls. ----------------------------------

/*!
 * \brief Displays message dialog.
 *
 * \param env the JNIEnv pointer
 * \param cls the OptionPane java class
 * \param messageType the message type
 * \param parentDelegate the parent componet
 * \param text the message text
 * \param title the dialog title
 * \param options the options
 * \param defaultOption the default option
 * 
 * \return selected option.
 */
jint OptionPaneDelegate::showMessage(JNIEnv *env, jclass cls, jint messageType,
                                     jlong parentDelegate, jstring text,
                                     jstring title, jint options,
                                     jint defaultOption)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(OptionPaneDelegate, showMessage, jint,
                                SWING_ENV_ARG, JCLASS_ARG(cls),
                                JINT_ARG(messageType),
                                JLONG_ARG(parentDelegate), JSTRING_ARG(text),
                                JSTRING_ARG(title), JINT_ARG(options),
                                JINT_ARG(defaultOption))
  QWidget *parent = 0;
  if (parentDelegate) {
    parent = COMPONENT_CAST(parentDelegate)->qWidget();
  }
  QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(options);
  QMessageBox::StandardButton defaultButton =
                                    QMessageBox::StandardButton(defaultOption);
  switch(messageType) {
    case MESSAGE_TYPE(ABOUT_MESSAGE):
      QMessageBox::about(parent, QStringAdapter(env, title),
                         QStringAdapter(env, text));
      break;
    case MESSAGE_TYPE(INFORMATION_MESSAGE):
      return QMessageBox::information(parent, QStringAdapter(env, title),
                                      QStringAdapter(env, text), buttons,
                                      defaultButton);
    case MESSAGE_TYPE(WARNING_MESSAGE):
      return QMessageBox::warning(parent, QStringAdapter(env, title),
                                  QStringAdapter(env, text), buttons,
                                  defaultButton);
    case MESSAGE_TYPE(QUESTION_MESSAGE):
      return QMessageBox::question(parent, QStringAdapter(env, title),
                                   QStringAdapter(env, text), buttons,
                                   defaultButton);
    case MESSAGE_TYPE(ERROR_MESSAGE):
      return QMessageBox::critical(parent, QStringAdapter(env, title),
                                   QStringAdapter(env, text), buttons,
                                   defaultButton);
  }
  return 0;
}
